package com.ruoyi.wcf.service.impl;

import java.util.*;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wcf.domain.WcfFitter;
import com.ruoyi.wcf.mapper.WcfFitterMapper;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wcf.mapper.WcfProductModelMapper;
import com.ruoyi.wcf.domain.WcfProductModel;
import com.ruoyi.wcf.service.IWcfProductModelService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 产品编号Service业务层处理
 *
 * @author WZP
 * @date 2022-04-02
 */
@Service
public class WcfProductModelServiceImpl implements IWcfProductModelService
{
    @Autowired
    private WcfProductModelMapper wcfProductModelMapper;
    @Autowired
    private WcfFitterMapper wcfFitterMapper;

    /**
     * 查询产品编号
     *
     * @param modelId 产品编号主键
     * @return 产品编号
     */
    @Override
    public WcfProductModel selectWcfProductModelByModelId(Long modelId)
    {
        return wcfProductModelMapper.selectWcfProductModelByModelId(modelId);
    }

    /**
     * 查询产品编号列表
     *
     * @param wcfProductModel 产品编号
     * @return 产品编号
     */
    @Override
    public List<WcfProductModel> selectWcfProductModelList(WcfProductModel wcfProductModel)
    {
        return wcfProductModelMapper.selectWcfProductModelList(wcfProductModel);
    }

    /**
     * 新增产品编号
     *
     * @param wcfProductModel 产品编号
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertWcfProductModel(WcfProductModel wcfProductModel)
    {

        //创建人
        String createBy = SecurityUtils.getUsername();
        Date createTime = new Date();

        //新增产品
        wcfProductModel.setStatus(0);//状态（0-待上架，1-上架）
        wcfProductModel.setCreateBy(createBy);
        wcfProductModel.setCreateAt(createTime);
        wcfProductModel.setDelFlag(0);
        wcfProductModelMapper.insertWcfProductModel(wcfProductModel);

        //添加产品适配
        List<WcfFitter> fitterList = wcfProductModel.getFitterList();

        if (!fitterList.isEmpty()){
            for (WcfFitter fitter : fitterList) {
                fitter.setProductId(wcfProductModel.getModelId());//产品id

                fitter.setCreateBy(createBy);
                fitter.setCreateAt(createTime);
                fitter.setDelFlag(0);
                wcfFitterMapper.insertWcfFitter(fitter);
            }
        }
        return 1;
    }

    /**
     * 修改产品编号
     *
     * @param wcfProductModel 产品编号
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateWcfProductModel(WcfProductModel wcfProductModel)
    {
        Date nowTime = new Date();

        wcfProductModel.setUpdateAt(nowTime);
        wcfProductModelMapper.updateWcfProductModel(wcfProductModel);

        //修改产品适配
        List<WcfFitter> fitterList = wcfProductModel.getFitterList();

        if (!fitterList.isEmpty()){
            //删除
            wcfFitterMapper.removeFitterByProductId(wcfProductModel.getModelId());

            for (WcfFitter fitter : fitterList) {
                //新增
                if (fitter.getFitterId() == null){
                    fitter.setProductId(wcfProductModel.getModelId());//产品id
                    fitter.setCreateBy(SecurityUtils.getUsername());
                    fitter.setCreateAt(nowTime);
                    fitter.setDelFlag(0);
                    wcfFitterMapper.insertWcfFitter(fitter);
                }else{
                    fitter.setUpdateAt(nowTime);
                    fitter.setDelFlag(0);
                    wcfFitterMapper.updateWcfFitter(fitter);
                }
            }
        }

        return 1;
    }

    /**
     * 批量删除产品编号
     *
     * @param modelIds 需要删除的产品编号主键
     * @return 结果
     */
    @Override
    public int deleteWcfProductModelByModelIds(Long[] modelIds)
    {
        return wcfProductModelMapper.deleteWcfProductModelByModelIds(modelIds);
    }

    /**
     * 删除产品编号信息
     *
     * @param modelId 产品编号主键
     * @return 结果
     */
    @Override
    public int deleteWcfProductModelByModelId(Long modelId)
    {
        return wcfProductModelMapper.deleteWcfProductModelByModelId(modelId);
    }

    /**
     * app-立即检索
     * @param param
     * @return
     */
    @Override
    public List<Map<String,Object>> searchProduct(Map<String,Object> param){
        List<Map<String, Object>> resultMap = new ArrayList<>();

        //选中手机型号列表
        List<Long> modelIdList = (List<Long>) param.get("modelIdList");

        //产品id列表
        List<Long> productIds = wcfFitterMapper.selectFitterProduct(modelIdList);
        System.out.println(productIds);
        if (!productIds.isEmpty()){
            List<Map<String, Object>> classId = wcfProductModelMapper.searchProduct(Long.parseLong(param.get("classId").toString()), productIds);

            for (Map<String, Object> map : classId) {
                System.out.println(map);

            }
        }

        return resultMap;
    }
}
